import 'package:finance_app/src/common/config.dart';
import 'package:intl/intl.dart';

class DateTimeUtils {
  static String formatGlobalDateTime(DateTime time) {
    if (time == null) return null;
    return formatDateTime(time, Config.settingInfo.dateFormat);
  }

  static String formatYear(DateTime time) {
    if (time == null) return null;
    return formatDateTime(time, 'yyyy');
  }

  static String formatMonth(DateTime time, [String format = 'MMMM']) {
    if (time == null) return null;
    return formatDateTime(time, format);
  }

  static String formatDateTime2Lines(DateTime time) {
    if (time == null) return null;
    return formatDateTime(time, 'd MMM\nyyyy');
  }

  static String formatDateTimeMMMYYYY(DateTime time) {
    if (time == null) return null;
    return formatDateTime(time, 'MMM yyyy');
  }

  static String formatDateTimeChatRoom(String time) {
    if (time == null) return null;
    DateTime dateTime = DateFormat('yyyy-MM-dd HH:mm:ss').parse(time);
    DateTime now = DateTime.now();
    if (now.year == dateTime.year &&
        now.month == dateTime.month &&
        now.day == dateTime.day) {
      return formatDateTime(dateTime, 'h:mm a');
    }
    return formatDateTime(dateTime, 'd MMM yyyy h:mm a');
  }

  static String formatDateTimeString(String time, [bool twolines = false]) {
    if (time == null) return null;
    DateTime dateTime = DateFormat('yyyy-MM-dd').parse(time);
    if (twolines) {
      return formatDateTime2Lines(dateTime);
    } else {
      return formatGlobalDateTime(dateTime);
    }
  }

  static String formatDateTimeFormat(String time, [String format]) {
    if (time == null) return null;
    DateTime dateTime = DateFormat('yyyy-MM-dd').parse(time);
    return DateFormat(format, 'en').format(dateTime);
  }

  static String formatFixedAssetDate(String startDate, String endDate) {
    if (startDate == null || endDate == null) return null;
    DateTime startDateTime = DateFormat('yyyy-MM-dd').parse(startDate);
    DateTime endDateTime = DateFormat('yyyy-MM-dd').parse(endDate);
    if (startDateTime.year == endDateTime.year) {
      return '${DateFormat('d MMM', 'en').format(startDateTime)} - ${DateFormat('d MMM yyyy', 'en').format(endDateTime)}';
    } else {
      return '${DateFormat('d MMM yyyy', 'en').format(startDateTime)} - ${DateFormat('d MMM yyyy', 'en').format(endDateTime)}';
    }
  }

  static String formatFinancialYearDate(String startDate, String endDate) {
    if (startDate == null || endDate == null) return null;
    DateTime startDateTime = DateFormat('yyyy-MM-dd').parse(startDate);
    DateTime endDateTime = DateFormat('yyyy-MM-dd').parse(endDate);

    return '${DateFormat('d MMM yyyy', 'en').format(startDateTime)} - ${DateFormat('d MMM yyyy', 'en').format(endDateTime)}';
  }

  static String formatDateTime(DateTime time, [String format]) {
    if (time == null) return null;
    return DateFormat(format, 'en').format(time);
  }

  static DateTime toDateTime(String time, [String format]) {
    if (time == null) return null;
    try {
      return DateFormat(format ?? 'yyyy-MM-dd HH:mm:ss').parse(time);
    } catch (e) {
      return null;
    }
  }

  static DateTime getLast3Month() {
    final current = DateTime.now();
    int year = current.year;
    int month = current.month;
    if (month - 3 <= 0) {
      year -= 1;
      month = 12 - month + 3;
    } else {
      month -= 3;
    }
    return DateTime(year, month, 1);
  }
}
